package com.shuoyue.ycgk.ui.course;

import android.os.Bundle;
import android.view.View;

import com.google.android.material.tabs.TabLayout;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.baseadapter.IndexFragmentStateAdapter;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.common.CommonModuleContract;
import com.shuoyue.ycgk.views.TabLayoutUtil;
import com.shuoyue.ycgk.views.viewpager_scroll.SuperViewPager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FragmentCourseTab extends BaseMvpFragment
        implements CommonModuleContract.View {

    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    SuperViewPager viewPager;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    CommonModuleContract.Presenter comPresenter;
    IndexFragmentStateAdapter indexFragmentStateAdapter;
    Type baseType;

    @Override
    public void initParams(Bundle bundle) {
        baseType = (Type) bundle.getSerializable("questionType");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_course_tab;
    }

    @Override
    protected void initView() {
        refreshlayout.setEnableLoadMore(false);
        refreshlayout.setOnRefreshListener(refreshLayout -> {
            comPresenter.getChildType(baseType.getId());
            refreshLayout.finishRefresh(2000);
        });
    }

    @Override
    protected void initData() {
        comPresenter = new CommonModuleContract.Presenter();
        comPresenter.attachView(this);
        comPresenter.getChildType(baseType.getId());
    }

    public static FragmentCourseTab getInstance(Type type) {
        FragmentCourseTab fragment = new FragmentCourseTab();
        Bundle bundle = new Bundle();
        bundle.putSerializable("questionType", type);
        fragment.setArguments(bundle);
        return fragment;
    }


    private void setType(List<Type> mTypes) {
        if (mTypes == null || mTypes.size() == 0) {
            tabLayout.setVisibility(View.GONE);
            List<BaseMvpFragment> fragmentMainItems = new ArrayList<>();
            fragmentMainItems.add(FragmengCourseItem.getInstance(baseType, baseType));
            viewPager.setOffscreenPageLimit(1);
            indexFragmentStateAdapter = new IndexFragmentStateAdapter(getChildFragmentManager(), fragmentMainItems);
            viewPager.setAdapter(indexFragmentStateAdapter);
            viewPager.setCurrentItem(0);
        } else {
            tabLayout.setVisibility(View.VISIBLE);
            List<BaseMvpFragment> fragmentMainItems = new ArrayList<>();
            for (Type topType : mTypes) {
                fragmentMainItems.add(FragmengCourseItem.getInstance(baseType, topType));
            }
            tabLayout.setupWithViewPager(viewPager);
            viewPager.setOffscreenPageLimit(1);
            indexFragmentStateAdapter = new IndexFragmentStateAdapter(getChildFragmentManager(), fragmentMainItems);
            viewPager.setAdapter(indexFragmentStateAdapter);
            viewPager.setCurrentItem(0);
            TabLayoutUtil tabLayoutUtil = new TabLayoutUtil();
            tabLayoutUtil.setTabTextSizeWhenSelect(tabLayout, mTypes);
        }
        //设置成功了,把刷新禁止了
        refreshlayout.setEnableRefresh(false);
    }

    @Override
    public void setBaseType(List<Type> baseTypes) {

    }

    @Override
    public void setTabType(List<Type> type) {
        setType(type);
    }

    public Type getCurrentType() {
        if (indexFragmentStateAdapter != null && indexFragmentStateAdapter.getCurrentFragment() != null) {
            FragmengCourseItem fragmengCourseItem = (FragmengCourseItem) indexFragmentStateAdapter.getCurrentFragment();
            return fragmengCourseItem.topType;
        }
        return null;
    }
}
